

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.lang.reflect.*;

public class Listener3 extends JFrame {

   public Listener3() {
      JButton open = new JButton("Open");
      JButton close = new JButton("Close");
      JButton save = new JButton("Save");
      getContentPane().setLayout(new FlowLayout());
      getContentPane().add(open);
      getContentPane().add(close);
      getContentPane().add(save);

      open.addActionListener(new ReflectiveAction(this, "open"));
      close.addActionListener(new ReflectiveAction(this, "close"));
      save.addActionListener(new ReflectiveAction(this, "save"));

      pack();
      setVisible(true);
   }

   public void open() {
      System.out.println("Open a file");
   }
   public void close() {
      System.out.println("Close a file");
   }
   public void save() {
      System.out.println("Save a file");
   }

   class ReflectiveAction implements ActionListener {
      String methodName;
      Object target;
      public ReflectiveAction(Object target, String methodName) {
         this.target = target;
         this.methodName = methodName;
      }

      public void actionPerformed(ActionEvent e) {
         try {
            Class[] argTypes = {};
            Method method = target.getClass().getMethod(methodName, argTypes);
            Object[] args = {};
            method.invoke(target, args);
         } catch (Exception ex) {
            ex.printStackTrace();
         }
      }
   }


   public static void main(String[] args) {
      new Listener3();
   }

}
